//
//  CWQ_HistoryLearnVC.m
//  BaxTer
//
//  Created by 陈武琦 on 16/9/2.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "CWQ_HistoryLearnVC.h"
#import "CWQ_courseIntroductionCell.h"
#import "BTCourseDetailVC.h"
#import "CWQ_tableView.h"

@interface CWQ_HistoryLearnVC ()
@property(nonatomic, strong)CWQ_tableView *tableView;
//上拉加载的页面
@property (nonatomic, assign) NSInteger page;
@property(nonatomic, strong)NSArray *dataSource;
@end

@implementation CWQ_HistoryLearnVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学习历史";
    CWQ_tableView *tableView = [[CWQ_tableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain cellClass:@"CWQ_courseIntroductionCell" vc:self upload:YES];
    tableView.loadedImageName = @"暂无学习历史";
    tableView.descriptionText = @"您还没有学习历史,赶紧开始学习吧!";
    [self.view addSubview:tableView];
    _dataSource = [NSMutableArray array];
    _page = 1;
    _tableView = tableView;
    [tableView cwq_requestDataWithDown:YES];
}

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.tableView setupSourceArray:self.dataSource];
}





#pragma -mark- --------------------获取学习历史----------------------

- (void)cwq_tableView:(CWQ_tableView *)tableView requestDataWithDown:(BOOL)down {
    
    if (down) {
        self.page = 1;
    }else {
        self.page++;
    }
    
    tableView.loading = YES;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @(self.page);
    params[@"rows"] = @(10);
    weakSelf_define;
    [[BTNetworkManager sharedMg] POST:BTStudyHistoryUrl parameters:params success:^(id responseObject) {
        
    if (responseObject_success) {
        NSMutableArray *arr = [BTCourseModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
        //判断是上拉刷新？  还是下拉加载？
        if (!down) {
            NSMutableArray *muArr = [weakSelf.dataSource mutableCopy];
            
            [muArr addObjectsFromArray:arr];
            weakSelf.dataSource = muArr;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]] || weakSelf.page == [responseObject[@"data"][@"totalPage"] integerValue]) {
                tableView.footer.hidden = YES;
            }
            [tableView.footer endRefreshing];
        }else {
            
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]] || weakSelf.page == [responseObject[@"data"][@"totalPage"] integerValue]) {
                tableView.footer.hidden = YES;
            }else {
                tableView.footer.hidden = NO;
            }
            self.dataSource = arr;
            [tableView.header endRefreshing];
        }
        if (self.dataSource.count<=0) {
            tableView.loading = NO;
        }
    }

    } failure:^(NSString *err) {
        
    }];

}

#pragma mark ----------------tableview代理--------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CWQ_courseIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CWQ_courseIntroductionCellID" forIndexPath:indexPath];
    //设置cell 的分割线
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.needShowGaizhang =YES;
    cell.courseModel = self.dataSource[indexPath.row];
   
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BTCourseModel *model = self.dataSource[indexPath.row];
    BTCourseDetailVC *courseDetailVC = [BTCourseDetailVC new];
    courseDetailVC.courseId = [model.id integerValue];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.navigationController pushViewController:courseDetailVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

@end
